package com.goat.model.element;

import com.goat.model.manager.GameLoader;

import javax.swing.*;
import java.awt.*;
import java.util.Map;
import java.util.Random;

public class Enemy extends BaseElement{
    private boolean left=true;
    private String direct="E_left";

    @Override
    public void showElement(Graphics g) {
        g.drawImage(getImageIcon().getImage(),getX(),getY(),getW(),getH(),null);
    }

    @Override
    public BaseElement createElement(String str) {
        Random random = new Random();
        int flag= random.nextInt(2);
        if(flag==0){
            left=false;
            direct="E_right";
        }

        String[] split = str.split(",");
        //0:x的值   1:y的值
        setX(Integer.parseInt(split[0]));
        setY(Integer.parseInt(split[1]));
        //获取图片管理器
        Map<String, ImageIcon> imageIconMap = GameLoader.getImageIconMap();
        ImageIcon imageIcon = imageIconMap.get(direct);//一开始向左或者向右
        setW(imageIcon.getIconWidth());
        setH(imageIcon.getIconHeight());
        setImageIcon(imageIcon);
        return this;
    }

    //会左右移动：要改变图片，改变状态（错，没有监听操作，直接改变数据），改变数据
    @Override
    protected void move() {
        if(direct.equals("E_left")){
            if(getX()>0){
                setX(getX()-3);//向左走
            }else {
                direct="E_right";//换方向
            }
        }else {
            if(getX()<850){
                setX(getX()+3);//向左走
            }else {
                direct="E_left";//换方向
            }
        }
    }

    @Override
    protected void updateImage(int gameTime) {
        //根据direct转换图片
        setImageIcon(GameLoader.getImageIconMap().get(direct));
    }
}
